<?php

namespace common\components\actions;

use yii\base\Action;
use yii\web\BadRequestHttpException;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use common\components\helpers\Fun;

/**
 * 后台通用排序Action
 * @author: 郭建飞
 * Date: 2015-3-11
 * Time: 11:11
 */

class ListorderAction extends Action{

    /**
     * @var string $param 排序记录时传递参数用的key
     */
    public $param='listorders';

    /**
     * @var string $modelClass 排序记录所对应的模型名称
     */
    public $modelClass;

    /**
     * @var array $message 排序操作执行后提示的结果信息
     */
    public $message=['success'=>'排序成功'];

    /**
     * @var string $redirectTo 排序后跳转到哪个控制器地址
     */
    public $redirectTo;

    /**
     * @var array $isHasChild 判断是否排序当前记录是否存在子记录
     */
    public $isHasChild=[];

    /**
     * @var array $redirectParam 排序后跳转到控制附带的参数
     */
    public $redirectParam=[];


    /**
     * 默认Action执行方法，重写父类方法
     */
    public function run(){
        $listorders=\Yii::$app->request->post($this->param);
        if (empty ( $listorders )||count($listorders)<1){
            throw new BadRequestHttpException('信息不存在');
        }
        $url=!empty($this->redirectParam)?Fun::url(ArrayHelper::merge([$this->redirectTo],$this->redirectParam)):Fun::url([$this->redirectTo]);
        foreach ($_POST['listorders'] as $k=>$v){
            $model_name=$this->modelClass;
            $model=$model_name::findOne($k);
            $model->listorder=$v;
            $model->save(false);
        }
        return	$this->controller->redirectMessage($this->message['success'], $url,true);
    }
}